package com.sun.dream.fish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.dream.fish.entity.TUserEntity;
import com.sun.dream.fish.mapper.TUserMapper;
import com.sun.dream.fish.mapstruct.UserMapBasic;
import com.sun.dream.fish.service.TUserService;
import com.sun.dream.fish.util.MD5Util;
import com.sun.dream.fish.util.PageUtil;
import com.sun.dream.fish.util.RandomNameUtil;
import com.sun.dream.fish.util.ResponseUtil;
import com.sun.dream.fish.vo.PageResultVO;
import com.sun.dream.fish.vo.ResultVO;
import com.sun.dream.fish.vo.back.UserVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljl
 * @since 2023-11-27
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUserEntity> implements TUserService {

    @Override
    public ResultVO<UserVO> saveUser(UserVO userVO) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ResponseUtil.fail(1000);
        }
        try {
            TUserEntity principal = (TUserEntity) subject.getPrincipal();
            TUserEntity tUserEntity = UserMapBasic.INSTANCE.voToEntity(userVO);
            tUserEntity.setCreatorId(principal.getId());
            tUserEntity.setUserId(MD5Util.encrypt(principal.getUsername() + principal.getPassword()));
            if (StringUtils.isBlank(tUserEntity.getName())) {
                tUserEntity.setName(RandomNameUtil.getCombinedRandom());
            }
            save(tUserEntity);
        } catch (Exception e) {
            log.error("[TUserService]异常, ", e);
            return ResponseUtil.fail(e.getMessage());
        }
        return ResponseUtil.ok("用户添加成功");
    }

    @Override
    public PageResultVO<UserVO> getUserList(String name, String status, Integer pageNum, Integer pageSize) {
        Page<TUserEntity> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        LambdaQueryWrapper<TUserEntity> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like(TUserEntity::getName, name);
        }
        if (StringUtils.isNotBlank(status)) {
            queryWrapper.like(TUserEntity::getStatus, Integer.parseInt(status));
        }
        Page<TUserEntity> newPage = page(page, queryWrapper);
        PageResultVO<UserVO> pageResultVO = new PageResultVO<>();
        pageResultVO.setData(PageUtil.pageToVo(newPage, UserMapBasic.class));
        return pageResultVO;
    }

    @Override
    public ResultVO<UserVO> updateUser(UserVO userVO) {
        try {
            TUserEntity tUserEntity = UserMapBasic.INSTANCE.voToEntity(userVO);
            tUserEntity.setUserId(null);
            tUserEntity.setUsername(null);
            tUserEntity.setDeleted(null);
            tUserEntity.setUserId(null);
            updateById(tUserEntity);
        } catch (Exception e) {
            log.error("【TUserService】异常, ", e);
            return ResponseUtil.fail("更新用户失败");
        }
        return ResponseUtil.ok("更新用户成功");
    }

    @Override
    public ResultVO<UserVO> deleteUser(Integer id) {
        try {
            removeById(id);
        } catch (Exception e) {
            log.error("【TUserService】异常, ", e);
            return ResponseUtil.fail("删除用户失败");
        }
        return ResponseUtil.ok("删除用户成功");
    }

    @Override
    public boolean checkUserExist(String username, String phoneNumber) {
        Objects.requireNonNull(username, "用户名不能为空");
        LambdaQueryWrapper<TUserEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TUserEntity::getUsername, username);
        if (StringUtils.isNotBlank(phoneNumber)) {
            queryWrapper.or().eq(TUserEntity::getTelephone, phoneNumber);
        }
        long count = count(queryWrapper);
        return count > 0;
    }
}
